package com.cloud.pole.shop.controller.order.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.shop.biz.order.ShopOrderBiz;
import com.cloud.pole.shop.model.entity.order.ShopOrder;
import com.cloud.pole.shop.model.entity.order.ShopOrderAdmin;
import com.cloud.pole.shop.service.order.OrderService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 商家管理订单
 * @author ThinkPad
 *
 */
@RestController
@RequestMapping("/order/order/admin_merch")
@Slf4j
public class OrderAdminMerchantController extends BaseController<ShopOrderBiz,ShopOrder>{
	
	@Autowired
    private OrderService orderService;
	
	/**
	 * 分页查询订单
	 * @param pageNo
	 * @param pageSize
	 * @param mobile
	 * @param templateName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
    @ResponseBody
    public ApiRestResponse<ShopOrderAdmin> list(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
    		@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
    		@RequestParam(name="searchType", defaultValue="") String searchType,
    		@RequestParam(name="orderSn", defaultValue="") String orderSn,
    		@RequestParam(name="deliName", defaultValue="") String deliName,
    		@RequestParam(name="deliTel", defaultValue="") String deliTel,
    		@RequestParam(name="startTime", defaultValue="") String startTime,
    		@RequestParam(name="endTime", defaultValue="") String endTime){
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("searchType", searchType);
			params.put("orderSn", orderSn);
			params.put("deliName", deliName);
			params.put("deliTel", deliTel);
			params.put("startTime", startTime);
			params.put("endTime", endTime);
			PageInfo<ShopOrderAdmin> pageInfo = orderService.selectPageShopOrderAdminVoList(token, params, pageNo, pageSize);
			return ApiRestResponse.successOfPage("成功", (int)pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageSize(),
					pageInfo.getList());
		} 
		catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} 
		catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
    }
	
	@RequestMapping(value = "/getOrderInfo/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ApiRestResponse<ShopOrderAdmin> getOrderInfo(
    		@RequestHeader(defaultValue = "") String token,
    		@PathVariable String id){
		try {
			ShopOrderAdmin shopOrder = null;
			String msg = "成功";
			if(id != null){
				shopOrder = orderService.selectOrderAdminById(token, id);
			}else{
				msg = "订单编号不能为空";
			}
			return ApiRestResponse.success(msg, shopOrder);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
    }
}
